package info.itest.www.lib;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;

public class LibCreate {
	WebDriver dr;
	String creatUrl = "http://localhost/wordpress/wp-admin/post-new.php";
	
	public LibCreate(WebDriver dr){
		this.dr = dr;
	}
	
	
	/*public String createPost(String title,String content){
		
		//this.dr.get("http://localhost/wordpress/wp-admin/post-new.php");
		//this.dr.get(creatUrl);
		//String title = "Test title" + System.currentTimeMillis();
		//this.dr.findElement(By.name("post_title")).clear();
		this.dr.findElement(By.name("post_title")).sendKeys(title);
		this.setContent(content);
		dr.findElement(By.name("publish")).click();
		return this.fetchPostId();
	}*/
	
	public String fetchPostId(){
		String postUrl = this.dr.findElement(By.id("sample-permalink")).getText();
		String[] arr = postUrl.split("=");
		return arr[1];
	}
	
	public String createPost(){
		String title = "Test title" + System.currentTimeMillis();
		String content = "Test Content" + System.currentTimeMillis();
		return this.createPost(title,content);
	}
	
	public String createPost(String title){
		String content = "test Content" + System.currentTimeMillis();
		return this.createPost(title,content);
	}

	public void setContent(String content) {

		String js = "document.getElementById('content_ifr').contentWindow.document.body.innerHTML='"
				+ content + "'";
		((JavascriptExecutor) dr).executeScript(js);
	}
	
	public String createPost(String title,String content){
		dr.findElement(By.name("post_title")).clear();
		this.setContent(null);
		dr.findElement(By.name("post_title")).sendKeys(title);
		this.setContent(content);
		dr.findElement(By.id("publish")).click();
		return this.fetchPostId();
	}
}
